﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;

namespace RooHui.OA.Biz
{
    public class SystemLogBiz
    {
        /// <summary>
        /// 客户端ip(访问用户)
        /// </summary>
        public static string GetUserIp
        {
            get
            {
                string realRemoteIP = "";
                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];
                }
                if (string.IsNullOrEmpty(realRemoteIP))
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (string.IsNullOrEmpty(realRemoteIP))
                {
                    realRemoteIP = System.Web.HttpContext.Current.Request.UserHostAddress;
                }
                return realRemoteIP;
            }
        }
        //System.Web.HttpContext.Current.User.Identity.Name
        public static void SystemLogAdd(string userName, SystemMsgType msgType, string message)
        {
            try
            {
                var systemlog = new SystemLog() { OperateUser = userName, MsgType = (int)msgType, Message = message + "-IP:"+SystemLogBiz.GetUserIp, CreateTime = DateTime.Now };
                systemlog.PostAdd();

            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
        }
    }
}
